<template>
  <div class="con">
    <twoHead></twoHead>
    <van-search
      class="search"
      v-model="value"
      shape="round"
      :placeholder="showKeyword"
      @search="onSearch"
    />
    <div v-if="arr.length <= 0">
      <div class="top-tit"><h5 class="tit">热搜榜</h5></div>
      <ul class="slist">
        <li :class="i <= 2 ? 'active' : ''" v-for="(v, i) in hotList" :key="i">
          <span>{{ i + 1 }}</span>
          <p class="van-ellipsis">{{ v.first }}</p>
        </li>
      </ul>
    </div>
    <div class="btm-con" v-else>
      <div
        class="btm-con-item"
        @click="addSong(v.id, index)"
        v-for="(v, index) in arr"
        :key="index"
      >
        <div class="btm-con-item-info">
          <h5 class="van-ellipsis song">{{ v.name }}</h5>
          <p class="van-ellipsis singer">
            <span v-for="(l, idx) in v.artists" :key="idx"
              >{{ l.name
              }}<span v-if="idx < v.artists.length - 1">/</span></span
            >
          </p>
        </div>
        <van-icon name="share-o" size="18" />
      </div>
    </div>
  </div>
</template>

<script>
import { mapState,mapMutations } from "vuex";

export default {
  data() {
    return {
      value: "",
      showKeyword: "",
      hotList: [],
      arr: [],
      add: {},
    };
  },
  computed:{
    ...mapState(['list','current']),
  },
  methods: {
    ...mapMutations(['setCurrent','setSxy','setShowPlayer']),
    addSong(id, i) {
      this.abb = i;
      this.$ajax.getMusic({ ids: id }).then((res) => {
        res.songs.forEach((item, i) => {
          this.add = { txt: item.name, pic: "", name: "", url: "" };
          this.add.pic = item.al.picUrl.replace(
            /^\/\d\.jpg$/,
            item.al.pic_str
          );
          item.ar.forEach((value) => {
            this.add.name += value.name + "/";
          });
          this.add.name = this.add.name.slice(
            0,
            this.add.name.length - 1
          );
        });
        this.$ajax.getMusicUrl({ id: id }).then((res) => {
          this.add.url = res.data[0].url;
          if(this.list.length>0){
              this.list.splice(this.current+1,0,this.add);
              this.setCurrent(this.current+1);
          }else{
              this.list.push(this.add);
              this.setSxy(true);
          }
          this.$nextTick(()=>{
            this.setShowPlayer(true);
          })
        });
      });
    },
    onSearch(val) {
      this.$ajax.getSearch({ keywords: val }).then((res) => {
        this.arr = res.result.songs;
      });
    },
  },
  mounted() {
    this.$ajax.getDefault().then((res) => {
      this.showKeyword = res.data.showKeyword;
    });
    this.$ajax.getSearchHot().then((res) => {
      this.hotList = res.result.hots;
    });
  },
};
</script>


<style lang="scss" scoped>
.song {
  font-size: 14px;
  color: #000;
}
.singer {
  font-size: 12px;
  color: #999;
}
.btm {
  width: 100%;
  background: #fefefe;
  color: #000;
  padding-bottom: 45px;
  &-con {
    padding: 10px 20px;
    &-item {
      h5 {
        font-weight: normal;
      }
      p {
        color: #666;
      }
      padding: 10px 0;
      display: flex;
      justify-content: space-between;
      align-items: center;
      text-align-last: left;
      &-num {
        font-size: 16px;
        color: #999;
      }
      &-info {
        width: 80%;
      }
    }
  }
  h5 {
    font-size: 14px;
  }
  &-count {
    font-size: 12px;
    color: #999;
  }
  &-tit {
    display: flex;
    justify-content: space-between;
    padding: 10px 20px;
  }
  &-l {
    width: 35%;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  &-r {
    width: 15%;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}

.con {
  padding-top: 45px;
  height: 100%;
  background: #fff;
  box-sizing: border-box;
}
.search {
  width: 70%;
  height: 28px;
  position: fixed;
  z-index: 100;
  top: 8px;
  width: 75%;
  left: 18%;
  background: #fff;
}
.top-tit {
  background: #fff;
}
.tit {
  font-size: 16px;
  color: #000;
  margin: 0 20px;
  border-bottom: 1px solid #eee;
  padding: 10px 0;
}
.slist {
  display: flex;
  flex-wrap: wrap;
  padding: 10px 20px;
  background: #fff;
  li {
    width: 50%;
    display: flex;
    padding: 10px;
    box-sizing: border-box;
    align-items: center;
  }
  font-size: 14px;
  span {
    color: #ccc;
    font-size: 16px;
    margin-right: 10px;
  }
  p {
    color: #333;
    width: 70%;
    text-align: left;
  }
}
.active {
  span {
    color: #f00;
  }
  p {
    color: #000;
    font-weight: bold;
  }
}
</style>